package com.example.qrcodedemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.qrcodedemo.utils.EncodingUtils;

public class MainActivity extends AppCompatActivity {
    private ImageView enCodeImage1,enCodeImage2;//展示生成的二维码
    private EditText editText;//输入要生成二维码的内容
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        enCodeImage1 = (ImageView) findViewById(R.id.code_image1);
        enCodeImage2 = (ImageView) findViewById(R.id.code_image2);
        editText = (EditText) findViewById(R.id.input_txt);
    }
    /**
     * 生成不带Logo的二维码点击事件
     * @param view
     */
    public void qrCode1(View view){
        if ("".equals(editText.getText().toString())){
            Toast.makeText(this, "请在输入框中输入内容", Toast.LENGTH_SHORT).show();
            return;
        }
        //生成二维码
        Bitmap codeBitmap = EncodingUtils.createQRCode(editText.getText().toString(),500,500,null);
        enCodeImage1.setImageBitmap(codeBitmap);//显示二维码
    }
    /**
     * 生成带Logo的二维码
     * @param view
     */
    public void qrCode2 (View view){
        if ("".equals(editText.getText().toString())){
            Toast.makeText(this, "请在输入框中输入内容", Toast.LENGTH_SHORT).show();
            return;
        }
        //获取logo资源,
        //R.drawable.logo为logo图片
        Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.logo);
        //生成二维码
        Bitmap codeBitmap = EncodingUtils.createQRCode(editText.getText().toString(),500,500,logoBitmap);
        enCodeImage2.setImageBitmap(codeBitmap);//显示二维码
    }
}
